package cn.xw.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

/**
 * @Auther: xiaoYang
 * @Date: 2021/4/11 17:55
 * @Description:
 */
@SpringBootConfiguration    //声明为配置类
@MapperScan(basePackages = "cn.xw.mapper")  //映射包扫描
public class MybatisPlusConfig {

    //Mybatis_plus拦截器
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        //创建插件核心类对象（拦截器）
        MybatisPlusInterceptor plusInterceptor = new MybatisPlusInterceptor();

        //创建乐观锁插件  对更新生效  并添加到核心插件对象实力里
        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor
                = new OptimisticLockerInnerInterceptor();
        plusInterceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);

        //创建 防止全表更新与删除插件（顺序必须在乐观锁插件后面）并添加到核心插件对象实力里
        BlockAttackInnerInterceptor blockAttackInnerInterceptor = new BlockAttackInnerInterceptor();
        plusInterceptor.addInnerInterceptor(blockAttackInnerInterceptor);

        //返回
        return plusInterceptor;
    }
}
